package com.example.egnotice1.controller;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.example.egnotice1.mapper.NoticeMapper;
import com.example.egnotice1.pojo.entity.Notice;
import com.example.egnotice1.pojo.tdo.NoticeAddPrarm;
import com.example.egnotice1.pojo.tdo.NoticeUpdatePrarm;
import com.example.egnotice1.pojo.vo.NoticeDetailInfoVO;
import com.example.egnotice1.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    //1.新增资讯功能
    @PostMapping("/v1/notice/add")
    @ResponseBody
    public String add(NoticeAddPrarm noticeAddPrarm) {
        System.out.println(noticeAddPrarm);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddPrarm,notice);
        notice.setCreate_time(new Date());
        notice.setUpdate_time(new Date());
        notice.setUser_id(1L);
        noticeMapper.add(notice);
        return "添加成功";
    }
    //2.资讯列表功能
    @GetMapping("/v1/notice/list")
    @ResponseBody
    public List<NoticeListVO> list() {
        List<NoticeListVO> list = noticeMapper.list();
        return list;
    }
    //3.资讯详情功能
    @GetMapping("/v1/notice/detail")
    @ResponseBody
    public NoticeDetailInfoVO detail(Integer id) {
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.detail(id);
        return noticeDetailInfoVO;
    }
    //4.删除资讯功能
    @PostMapping("/v1/notice/del")
    @ResponseBody
    public String del(Integer id){
        noticeMapper.del(id);
        return "删除公告成功";
    }
    //5. 更新资讯功能
    @PostMapping("/v1/notice/update")
    @ResponseBody
    public String update(NoticeUpdatePrarm noticeUpdatePrarm){
        noticeMapper.update(noticeUpdatePrarm);
        return "更新资讯成功";
    }
}
